<?php
class system{
    public static function Run(){
        call_user_func("system::".$_SERVER["REQUEST_METHOD"]."_".$_GET["p"]);
    }

    public static function GET_list(){
        if(!file_exists($_GET["s_project_path"]."/data/demand.json")){
            echo json_encode([
                "res"=>"OK","list"=>[]
            ]);
            exit;
        }
        $data=json_decode(file_get_contents($_GET["s_project_path"]."/data/demand.json"),true);
        echo json_encode([
            "res"=>"OK","list"=>$data
        ]);
    }

    public static function POST_add(){
        if(!is_dir($_POST["s_project_path"])){
            // echo $_POST["s_project_path"];
            mkdir($_POST["s_project_path"],0777);
            mkdir($_POST["s_project_path"]."/data",0777);
        }
        if(!file_exists($_POST["s_project_path"]."/data/demand.json")){
            file_put_contents($_POST["s_project_path"]."/data/demand.json","[]");
        }
        $data=json_decode(file_get_contents($_POST["s_project_path"]."/data/demand.json"),true);
        array_push($data,[
            "id"=>count($data)+1,
            "s_demand_desc"=>$_POST["s_demand_desc"],
            "s_demand_data"=>$_POST["s_demand_data"],
            "s_demand_list"=>$_POST["s_demand_list"],
            "i_drop"=>0
        ]);
        file_put_contents($_POST["s_project_path"]."/data/demand.json",json_encode($data,JSON_UNESCAPED_UNICODE));
        echo json_encode([
            "res"=>"OK"
        ],JSON_UNESCAPED_UNICODE);
    }

    public static function POST_edit(){
        if(!file_exists($_POST["s_project_path"]."/data/demand.json")){
            echo json_encode([
                "res"=>"NG","message"=>"无项目更新"
            ]);
        }
        $data=json_decode(file_get_contents($_POST["s_project_path"]."/data/demand.json"),true);
        // print_r($data);
        $data[$_POST["id"]-1]["s_demand_desc"]=$_POST["s_demand_desc"];
        $data[$_POST["id"]-1]["s_demand_data"]=$_POST["s_demand_data"];
        $data[$_POST["id"]-1]["s_demand_list"]=$_POST["s_demand_list"];
        file_put_contents($_POST["s_project_path"]."/data/demand.json",json_encode($data,JSON_UNESCAPED_UNICODE));
        echo json_encode([
            "res"=>"OK"
        ],JSON_UNESCAPED_UNICODE);
    }

    public static function POST_delete(){
        if(!file_exists($_POST["s_project_path"]."/data/demand.json")){
            echo json_encode([
                "res"=>"NG","message"=>"无项目更新"
            ]);
        }
        $data=json_decode(file_get_contents($_POST["s_project_path"]."/data/demand.json"),true);
        // print_r($data);
        $data[$_POST["id"]-1]["i_drop"]=1;
        file_put_contents($_POST["s_project_path"]."/data/demand.json",json_encode($data,JSON_UNESCAPED_UNICODE));
        echo json_encode([
            "res"=>"OK"
        ],JSON_UNESCAPED_UNICODE);
    }
}

system::Run();
?>